package com.rgs.platform.service.impl;

import cn.hutool.core.util.IdUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.rgs.base.core.page.IPageRequestModel;
import com.rgs.base.database.DataPageUtil;
import com.rgs.platform.entity.BillInfoDO;
import com.rgs.platform.filter.BillInfoFilterDTO;
import com.rgs.platform.mapper.BillInfoMapper;
import com.rgs.platform.service.BillInfoService;
import com.rgs.platform.test.RgsSqlUtil;
import joptsimple.internal.Strings;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;

/**
 * @author Ren Gaoshuai
 * @ClassName BillInfoServiceImpl
 * @date 2022-01-24 11:17
 **/
@Service
public class BillInfoServiceImpl implements BillInfoService {

    @Resource
    BillInfoMapper billInfoMapper;

    @Override
    public Optional<BillInfoDO> selectByFilter(BillInfoFilterDTO billInfoFilter) {
        Example example = RgsSqlUtil.joinSql(BillInfoDO.class, billInfoFilter);
        return billInfoMapper.selectByExample(example).stream().findFirst();
    }

    @Override
    public List<BillInfoDO> listByFilter(BillInfoFilterDTO billInfoFilter) {
        Example example = RgsSqlUtil.joinSql(BillInfoDO.class, billInfoFilter);
        return billInfoMapper.selectByExample(example);
    }

    @Override
    public PageInfo<BillInfoDO> listPageByFilter(IPageRequestModel iPageRequestModel, BillInfoFilterDTO billInfoFilter) {
        Example example = RgsSqlUtil.joinSql(BillInfoDO.class, billInfoFilter);
        return DataPageUtil.selectPageList(iPageRequestModel, () -> billInfoMapper.selectByExample(example));
    }

    @Override
    public BillInfoDO selectById(String id) {
        return billInfoMapper.selectByPrimaryKey(id);
    }

    @Override
    public BillInfoDO insert(BillInfoDO billInfoDO) {
        if (Strings.isNullOrEmpty(billInfoDO.getId())) {
            billInfoDO.setId(IdUtil.fastSimpleUUID());
        }
        billInfoDO.setCreateTime(LocalDateTime.now());
        billInfoMapper.insert(billInfoDO);
        return billInfoDO;
    }

    @Override
    public void deleteByFilter(BillInfoFilterDTO billInfoFilter) {
        Example example = RgsSqlUtil.joinSql(BillInfoDO.class, billInfoFilter);
        billInfoMapper.deleteByExample(example);
        return;
    }

    @Override
    public void deleteById(String id) {
        billInfoMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void updateSelectiveByPrimaryKey(BillInfoDO billInfoDO) {
        billInfoMapper.updateByPrimaryKeySelective(billInfoDO);
    }
}
